﻿using System;
using System.IO;
using System.Web;
using System.Web.Mvc;

namespace QG.FrontDesk.com.Controllers
{
    public class FileUploadController : Controller
    {
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UploadImage(HttpPostedFileBase file)
        {
            //先定义文件
            string fileName = Guid.NewGuid().ToString();
           
            string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Pic");
            //如果没有图片
            if (Request.Files.Count == 0)
            {
                return Json(new { Success = false, FilePath = "" });
            }
            //获取图片的扩展名
            string stuf = Path.GetExtension(file.FileName);

            //图片存放路径不存在则创建
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            //保存图片
            string path = Path.Combine(filePath, fileName + stuf);
            file.SaveAs(path);
            string ulr = "/Pic/" + fileName + stuf;
            //返回前端
            return Json(new { Success = true, Url = ulr });

        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public JsonResult Upload(HttpPostedFileBase file)
        {
            var fileName = file.FileName;
            var filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "UploadFiles"); 
            if (Request.Files.Count == 0)
            {
                return Json(new { Success = false, FilePath = "" });
            }
            file.SaveAs(Path.Combine(filePath, fileName));
            return Json(new { Success = true, Url = filePath });
        }

    }
}